package sia.tp1;

import java.util.ArrayList;
import java.util.List;

import sia.tp1.genotype.Genotype;
import sia.tp1.operator.And;
import sia.tp1.operator.Constant;
import sia.tp1.operator.Not;
import sia.tp1.operator.Operator;
import sia.tp1.operator.Or;

public class Problem {
	
	static Integer solution = 64;

	public static Operator generateConstantOperator() {
		
		return new Constant((int)(Math.random()*6));
	}

	public static Operator generateOperator() {
		double randValue = Math.random();
		if(randValue<0.33) {
			return new And();
		}
		if(randValue<0.66) {
			return new Or();
		}
		return new Not();
	}
	
	public static Integer aptitude(Genotype genotype) throws Exception {
		List<Boolean> input = new ArrayList<Boolean>();
		Integer rta = 0;
		boolean partial;
		
		//Menos de 20 nodos pondera muy mal
		if( genotype.getNodesCount() < 20 )
			return 0;

		for(int i0=0; i0 < 2; i0++)
			for(int i1=0; i1 < 2; i1++)
				for(int i2=0; i2 < 2; i2++)
					for(int i3=0; i3 < 2; i3++)
						for(int i4=0; i4 < 2; i4++)
							for(int i5=0; i5 < 2; i5++) {
								input.clear();
								
								switch (i0) {
									case 0: input.add(false); break;
									case 1: input.add(true); break;
								}
								switch (i1) {
									case 0: input.add(false); break;
									case 1: input.add(true); break;
								}
								switch (i2) {
									case 0: input.add(false); break;
									case 1: input.add(true); break;
								}
								switch (i3) {
									case 0: input.add(false); break;
									case 1: input.add(true); break;
								}
								switch (i4) {
									case 0: input.add(false); break;
									case 1: input.add(true); break;
								}
								switch (i5) {
									case 0: input.add(false); break;
									case 1: input.add(true); break;
								}
								
								partial = genotype.eval(input);

								switch (i0) {
									case 0: switch (i1) {
												case 0: if( input.get(2) == partial ) rta++; break;
												case 1: if( input.get(3) == partial ) rta++; break;
											}; break;
									case 1: switch (i1) {
												case 0: if( input.get(4) == partial ) rta++; break;
												case 1: if( input.get(5) == partial ) rta++; break;
											}; break;
								}
							}
		if (rta < solution)
			return rta;
		else {
			return rta+(1000/genotype.getNodesCount());
		}

	}

}
